package com.itic.web.mem.pointwater.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itic.appbase.applications.sys.common.service.OutCommonService;
import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.ht.cms.exam.persistence.model.Exam;
import com.itic.ht.cms.exam.service.ExamService;
import com.itic.ht.mem.member.persistence.model.WebMember;
import com.itic.ht.mem.member.service.WebMemberService;
import com.itic.ht.mem.pointexecutetype.persistence.model.WebPointExecuteType;
import com.itic.ht.mem.pointexecutetype.persistence.model.WebPointExecuteTypeExample;
import com.itic.ht.mem.pointexecutetype.service.WebPointExecuteTypeService;
import com.itic.ht.mem.pointoffer.persistence.model.PointOffer;
import com.itic.ht.mem.pointoffer.persistence.model.PointOfferExample;
import com.itic.ht.mem.pointoffer.service.PointOfferService;
import com.itic.ht.mem.pointwater.persistence.model.WebPointWater;
import com.itic.ht.mem.pointwater.service.WebPointWaterService;
import com.itic.ht.mem.vedio.persistence.model.WebVedio;
import com.itic.ht.mem.vedio.service.WebVedioService;
import com.itic.web.qo.TokenData;
import com.itic.web.ro.ResResult;
import com.itic.web.ro.WebCodeDetailVo;
import com.itic.web.util.TokenUtil;


@Service
@Transactional
public class AppPointWaterService {
    @Autowired
    private WebPointWaterService pointWaterService;
    @Autowired
    private PointOfferService pointOfferService;
	@Autowired
	private WebMemberService webMemberService;
	@Autowired
	private WebVedioService webVedioService;
    @Autowired
    private WebPointExecuteTypeService pointExecuteTypeService;
    @Autowired
	protected OutCommonService outCommonService;
    @Autowired
    private ExamService examService;
    
    private static final String CATEGORY = "T_WEB_POINT_ECECUTE_POINT_STATUS_CATEGORY";//代码表名称
    
    public ResResult add(WebPointWater temp,TokenData data){
		try {
			//判断token是不是存在
			if(StringHelper.isEmpty(data.getToken())){
				return ResResult.error(1, "token不能为空");
			}
			//先判断token正确性
			boolean flag = TokenUtil.assertUserToken(data);
			if(!flag){
				return ResResult.error(2, "token失效");
			}
			//判断Item是不是存在
			if(StringHelper.isEmpty(temp.getItemId())){
				return ResResult.error(3, "项目ID不能为空");
			}
			
			//从token中取account信息   
			data.setAccount(TokenUtil.getUserList(data.getToken()).getAccount().trim());
			//取得用户信息
			WebMember member = webMemberService.getMemberByAccount(data.getAccount());
			System.out.println("现在要查询的帐号是:"+member.getAccount());
			
			temp.setAccount(member.getAccount());
			if(temp.getCategory().equals("001")){
				//查询视频积分
				WebVedio webVedio = webVedioService.get(temp.getItemId());
				temp.setExecutePoints(webVedio.getBonusPoints());
			}
			if(temp.getCategory().equals("002")){
				//查询考试积分
				Exam exam = examService.get(temp.getItemId());
				temp.setExecutePoints(exam.getBonusPoints());
			}
			
			//添加流水
			String result = pointWaterService.add(temp);
			//更新成功后给用户更新自己的积分
			member.setSumPoints(member.getSumPoints() + temp.getExecutePoints());
			webMemberService.edit(member);
			
			return ResResult.build(200, "添加积分流水成功", result);
		} catch (Exception e) {
			//返回错误代码
			return ResResult.error(4, "添加积分流水失败");
		}
    }
    public ResResult addOffer(PointOffer temp,TokenData data){
    	try {
    		//判断token是不是存在
    		if(StringHelper.isEmpty(data.getToken())){
    			return ResResult.error(1, "token不能为空");
    		}
    		//先判断token正确性
    		boolean flag = TokenUtil.assertUserToken(data);
    		if(!flag){
    			return ResResult.error(2, "token失效");
    		}
    		//判断时段是不是存在
    		if(StringHelper.isEmpty(temp.getTimeSlot())){
    			return ResResult.error(3, "时段不能为空");
    		}
			//判断Item是不是存在
			if(StringHelper.isEmpty(temp.getCategory())){
				return ResResult.error(3, "项目ID不能为空");
			}

    		
    		//从token中取account信息   
    		data.setAccount(TokenUtil.getUserList(data.getToken()).getAccount().trim());
    		//取得用户信息
    		WebMember member = webMemberService.getMemberByAccount(data.getAccount());
    		System.out.println("现在要查询的帐号是:"+member.getAccount());
    		//积分获取分析
    		Integer executePoints = pointExecuteTypeService.get(temp.getCategory()).getPoints();
    		System.out.println("申请分数:"+executePoints);
    		System.out.println("剩余分数:"+executePoints);
    		if(executePoints > (member.getSumPoints() - member.getExchangePoints())){
    			return ResResult.error(5, "您的积分不够兑换此业务");
    		}
    		//添加兑换申请
    		temp.setAccount(member.getAccount());
    		temp.setStatus("default");
    		temp.setExecutePoints(executePoints);
    		String result = pointOfferService.add(temp);
    		//更新成功后给用户更新自己的积分
    		member.setExchangePoints(member.getExchangePoints() + temp.getExecutePoints());
    		webMemberService.edit(member);
    		
    		return ResResult.build(200, "兑换申请成功", result);
    	} catch (Exception e) {
    		//返回错误代码
    		e.printStackTrace();
    		return ResResult.error(4, "兑换申请失败");
    	}
    }
    public ResResult getMyOfferList(TokenData data){
    	try {
    		//判断token是不是存在
    		if(StringHelper.isEmpty(data.getToken())){
    			return ResResult.error(1, "token不能为空");
    		}
    		//先判断token正确性
    		boolean flag = TokenUtil.assertUserToken(data);
    		if(!flag){
    			return ResResult.error(2, "token失效");
    		}
    		
    		//从token中取account信息   
    		data.setAccount(TokenUtil.getUserList(data.getToken()).getAccount().trim());
    		//取得用户信息
    		WebMember member = webMemberService.getMemberByAccount(data.getAccount());
    		System.out.println("现在要查询的帐号是:"+member.getAccount());
    		
    		
    		//添加兑换申请
    		PointOfferExample pointOfferExample = new PointOfferExample();
    		pointOfferExample.createCriteria().andAccountEqualTo(member.getAccount()).andDelFlagEqualTo(Integer.parseInt(DBConst.TRUE));
    		List<PointOffer> list = pointOfferService.list(pointOfferExample);
    		if(list != null && list.size()>0){
    			for (PointOffer pointOffer : list) {
    				if(pointOffer.getCategory()!=null && !"".equals(pointOffer.getCategory())){
    					pointOffer.setCategory(pointExecuteTypeService.get(pointOffer.getCategory()).getName());
    				}
            		if(pointOffer.getStatus()!=null && !"".equals(pointOffer.getStatus())){
            			pointOffer.setStatus(outCommonService.getNameByCode(CATEGORY, pointOffer.getStatus()));
            		}
				}
    			
    		}else{
    			list = new ArrayList<PointOffer>();
    		}
    		return ResResult.build(200, "我的兑换申请查询成功", list);
    	} catch (Exception e) {
    		//返回错误代码
    		return ResResult.error(4, "我的兑换申请查询失败");
    	}
    }
    
    public ResResult executeType(TokenData data){
    	try {
    		//判断token是不是存在
    		if(StringHelper.isEmpty(data.getToken())){
    			return ResResult.error(1, "token不能为空");
    		}
    		//先判断token正确性
    		boolean flag = TokenUtil.assertUserToken(data);
    		if(!flag){
    			return ResResult.error(2, "token失效");
    		}

    		
    		//从token中取account信息   
    		data.setAccount(TokenUtil.getUserList(data.getToken()).getAccount().trim());
    		//取得用户信息
    		WebMember member = webMemberService.getMemberByAccount(data.getAccount());
    		System.out.println("现在要查询的帐号是:"+member.getAccount());
    		

            // 声明返回对象
            List<WebCodeDetailVo> webCodeDetailVoList = new ArrayList<WebCodeDetailVo>();
            //根据表名查到parentId
            WebPointExecuteTypeExample codeExample = new WebPointExecuteTypeExample();
            codeExample.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE);
            List<WebPointExecuteType> list = pointExecuteTypeService.list(codeExample);
            
            if(list!=null && list.size()>0){
            	
                        // 封装数据
                        for (WebPointExecuteType executeType : list) {
                            WebCodeDetailVo vo = new WebCodeDetailVo();
                            vo.setId(executeType.getId());
                            vo.setCode(executeType.getId());
                            vo.setName(executeType.getName());
                            vo.setPoints(executeType.getPoints().toString());
                            webCodeDetailVoList.add(vo);
                        }
                    
            }
    		
    		return ResResult.build(200, "获取积分兑换类型成功", webCodeDetailVoList);
    	} catch (Exception e) {
    		//返回错误代码
    		return ResResult.error(4, "获取积分兑换类型失败");
    	}
    }


}